package org.jeecg.cn.edu.scau.vm.config;

import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
public class ContextClosedHandler implements ApplicationListener<ContextClosedEvent> {
    private static final Logger logger = LoggerFactory.getLogger(ContextClosedHandler.class);
    @Resource(name = "readStreamThreadPool")
    private ThreadPoolExecutor readStreamThreadPool;

    @Override
    public void onApplicationEvent(@NotNull ContextClosedEvent event) {
        logger.info("正在关闭线程池....");
        // 关闭线程池
        readStreamThreadPool.shutdown();
        // 关闭连接
        try {
            boolean b = readStreamThreadPool.awaitTermination(3, TimeUnit.SECONDS);
            logger.info("线程池关闭状态:{}", b);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


}